package com.czw.ali.register

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.Log
import com.alibaba.sdk.android.push.CloudPushService
import com.alibaba.sdk.android.push.CommonCallback
import com.alibaba.sdk.android.push.huawei.HuaWeiRegister
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
import com.alibaba.sdk.android.push.register.MeizuRegister
import com.alibaba.sdk.android.push.register.MiPushRegister
import com.didi.drouter.annotation.Service
import com.didi.drouter.api.Extend
import com.mtzqc.common.BuildConfig
import com.mtzqc.common.init.AgreeService
import com.mtzqc.common.task.ITask


@Service(
    function = [AgreeService::class],
    priority = 50,
    cache = Extend.Cache.SINGLETON
)

class PushAgreeRegister(
) : AgreeService {
    override val dependsLib: Array<Class<out ITask>>?
        get() = null
    override val depends: Array<String>?
        get() = null
    override val depName: String
        get() = "Push"
    override val priority: Int
        get() = 50

    override fun executeInit(app: Application, debug: Boolean, dev: Boolean): Boolean {
        if (initAgree) {
            return true
        }
        initAgree = true
        createChannel(app)
        PushServiceFactory.getCloudPushService()?.apply {
            setDebug(debug)
            if (debug) {
                setLogLevel(CloudPushService.LOG_DEBUG)
            }
            register(app, object : CommonCallback {
                override fun onSuccess(response: String?) {
                    Log.d("ddd", "init cloudchannel success " + response)
                }

                override fun onFailed(errorCode: String, errorMessage: String) {
                    Log.d(
                        "ddd",
                        "init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage"
                    )
                }

            })

        }

        MiPushRegister.register(
            app,
            BuildConfig.PUSH_XIAO_MI_APP_ID,
            BuildConfig.PUSH_XIAO_MI_APP_KEY
        )
        HuaWeiRegister.register(app)
        // VivoRegister.register(app);
        //  OppoRegister.register(app, appKey, appSecret);
        MeizuRegister.register(app, BuildConfig.PUSH_MEI_ZU_APP_ID, BuildConfig.PUSH_MEI_ZU_APP_KEY)
        return true
    }

    private fun createChannel(app: Application) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val mNotificationManager =
                app.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            mNotificationManager.createNotificationChannel(
                NotificationChannel(
                    BuildConfig.PUSH_ALI_CHANNEL,
                    "消息通知",
                    NotificationManager.IMPORTANCE_HIGH
                ).apply {
                    description = "消息推送信息"
                    enableLights(true)
                    lightColor = Color.RED
                    enableVibration(true)
                    vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
                })
        }
    }

    companion object {
        var initAgree = false
    }

}